package com.example.threadmode;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootApplication
public class ThreadLocalTest {

    private static ExecutorService service = Executors.newFixedThreadPool(1);

    public static void main(String[] args) {
        ThreadLocalTest threadLocalTest = new ThreadLocalTest();
        ThreadLocalTest threadLocalTest1 = threadLocalTest;
        threadLocalTest1.asd();

        InheritableThreadLocal<Boolean> threadLocal = new InheritableThreadLocal<>();

        threadLocal.set(Boolean.TRUE);

        service.execute(() -> {
            System.out.println("子线程 1 获取到主线程设置的值：" + threadLocal.get());
        });

        threadLocal.set(Boolean.FALSE);

        service.execute(() -> {
            threadLocal.set(Boolean.FALSE);
            System.out.println("子线程 2 获取到主线程设置的值：" + threadLocal.get());
        });

        service.shutdown();
    }
    private void asd() {
        try {
            File file = new File("C:\\Users\\ZhangYuGe\\Desktop\\金舟格式工厂\\Moon & Words - Main{AuraSound}{e56554a657ce49aa8969e8b94c6467de}.wav");
            if(file.exists()){
//            可以直接把文件放这里

                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
                Clip clip = AudioSystem.getClip();
                clip.open(audioInputStream);
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);
//                extracted(clip);
                always(clip);
                JOptionPane.showMessageDialog(null,"wati");
            } else{
                System.out.println("zhaobudao");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void extracted(Clip clip) {
        JOptionPane.showMessageDialog(null, "pluse");
//                将音乐的时间转为毫秒
        long clipMicrosecondPosition = clip.getMicrosecondPosition();
        clip.stop();
        JOptionPane.showMessageDialog(null, "continue");
        clip.setMicrosecondPosition(clipMicrosecondPosition);
        clip.start();
    }

    private void always(Clip clip){
        extracted(clip);
        always(clip);
        
    }
}